package com.stars.module.user.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseMapper;
import com.stars.base.entity.CommonDTO;
import com.stars.module.user.entity.StarOrgUser;
import com.stars.module.user.entity.StarUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author : Rocks
 * @version : 1.0
 * @date : 2022/10/18 10:07
 * @description : 用户查询映射
 */
@Mapper
public interface UserMapper extends MPJBaseMapper<StarUser> {

//    @Select("SELECT U.* FROM STAR_USER U LEFT JOIN STAR_ORGUSER O ON U.ID = O.USER_ID WHERE O.ORG_ID=#{fid} AND ( U.NAME LIKE CONCAT('%',#{search},'%') )  AND U.ROWSTATUS > -1 ORDER BY U.SHOWORDER limit #{current}, #{pageSize}")
//    List<StarUser> loadUserByDeptId(CommonDTO dto);

    @Select("SELECT U.* FROM STAR_USER U " +
            "LEFT JOIN STAR_ORGUSER O ON U.ID = O.USER_ID " +
            "WHERE O.ORG_ID=#{fid} AND ( U.NAME LIKE CONCAT('%',#{search},'%') )  AND U.ROWSTATUS > -1 AND U.ID <> '1' " +
//            "AND U.ID NOT IN (#{selectedData}) " +
            "ORDER BY U.SHOWORDER limit #{current}, #{pageSize}")
    List<StarUser> loadUserByDeptId(CommonDTO dto);

    @Select("SELECT U.* FROM STAR_USER U LEFT JOIN STAR_ORGUSER O ON U.ID = O.USER_ID WHERE O.ORG_ID=#{fid} AND ( U.NAME LIKE CONCAT('%',#{search},'%') )  AND U.ROWSTATUS > -1 ORDER BY U.SHOWORDER limit #{current}, #{pageSize}")
    List<StarUser> loadUserByDeptId(CommonDTO dto, Page<StarUser> page);

    @Select("SELECT COUNT(1) FROM STAR_USER U LEFT JOIN STAR_ORGUSER O ON U.ID = O.USER_ID WHERE O.ORG_ID=#{fid} AND ( U.NAME LIKE CONCAT('%',#{search},'%') )  AND U.ROWSTATUS > -1")
    int userCountByDeptId(CommonDTO dto);

    @Select("SELECT MAX(SHOWORDER) FROM STAR_ORGUSER WHERE DEPT_ID = #{fid}")
    Integer getMaxIndex(String fid);

    @Select("SELECT G.ID,U.NAME,OO.ORGNAME FROM STAR_USER U " +
            "LEFT JOIN STAR_GRANT G ON U.ID = G.TARGET_ID " +
            "LEFT JOIN STAR_ORGUSER O ON U.ID = O.USER_ID " +
            "INNER JOIN STAR_ORGANIZES OO ON O.ORG_ID = OO.ID " +
            "WHERE G.SOURCE_ID=#{fid} AND ( U.NAME LIKE CONCAT('%',#{search},'%') )  AND U.ROWSTATUS > -1 ORDER BY U.SHOWORDER limit #{current}, #{pageSize}")
    List<StarOrgUser> loadUserByRoleId(CommonDTO dto);

    @Select("SELECT COUNT(1) FROM STAR_USER U LEFT JOIN STAR_GRANT G ON U.ID = G.TARGET_ID WHERE G.SOURCE_ID=#{fid} AND ( U.NAME LIKE CONCAT('%',#{search},'%') )  AND U.ROWSTATUS > -1")
    int userCountByRoleId(CommonDTO dto);
}
